package com.cloudlinkscm.base.authcenter.dao;

import com.cloudlinkscm.base.authcenter.mapper.RolePermissionRelationMapper;
import com.cloudlinkscm.base.authcenter.model.RolePermissionRelation;
import com.cloudlinkscm.loms.framework.dao.GenericBizDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collection;
import java.util.List;

/**
 * @author : tac
 * @date : 28/07/2017
 */
@Repository
public class RolePermissionRelationDao extends GenericBizDao<RolePermissionRelation, String> {

    @Autowired
    public RolePermissionRelationDao(RolePermissionRelationMapper mapper) {
        super(mapper);
    }


	public RolePermissionRelation selectByRoleIdAndResourceId(String roleId, String resourceId) {
		RolePermissionRelation relation = new RolePermissionRelation();
		relation.setRoleId(roleId);
		relation.setResourceId(resourceId);
    	return selectOne(relation);
	}

	public List<RolePermissionRelation> selectByRoleIdAndResourceIds(String roleId, Collection<String> resourceIds) {
		Example example = new Example(RolePermissionRelation.class);
		example.createCriteria()
				.andEqualTo("roleId", roleId)
				.andIn("resourceId", resourceIds);
		return selectByExample(example);
	}

	public List<RolePermissionRelation> selectByRoleId(String roleId) {
		RolePermissionRelation relation = new RolePermissionRelation();
		relation.setRoleId(roleId);
		return select(relation);
	}


	public List<RolePermissionRelation> selectByRoleIds(Collection<String> roleIds) {
		Example example = new Example(RolePermissionRelation.class);
		example.createCriteria()
				.andIn("roleId", roleIds);
		return selectByExample(example);
	}


	public int deleteByIds(Collection<String> ids) {
		Example example = new Example(RolePermissionRelation.class);
		example.createCriteria()
				.andIn("resourceId", ids);
		return deleteByExample(example);
	}
}
